#include <stdio.h>
#include <string.h>

#include "table.h"
#include "list.h"
#include "record.h"

int
main(int argc, char *argv[])
{
	if (init_database() == -1)
	{
		puts("Init Failed!");
		return 0;
	}
	else
		puts("Successfully Init!");

	struct TABLE test = {"test", 3};
	strcpy(test.sfaFields[0].szFieldName, "id");
	test.sfaFields[0].iType = INT_TYPE;
	test.sfaFields[0].bIsKey = 1;
	test.sfaFields[0].bIsNull = 0;

	strcpy(test.sfaFields[1].szFieldName, "name");
	test.sfaFields[1].iType = STR_TYPE;
	test.sfaFields[1].iLength = 10;
	test.sfaFields[1].bIsKey = 1;
	test.sfaFields[1].bIsNull = 0;

	strcpy(test.sfaFields[2].szFieldName, "whatever");
	test.sfaFields[2].iType = STR_TYPE;
	test.sfaFields[2].iLength = 10;
	test.sfaFields[2].bIsKey = 1;
	test.sfaFields[2].bIsNull = 1;

	show_tables();
	create_table(test);
	show_tables();
	desc_table("test");
	drop_table("test");
	show_tables();
	create_table(test);
	show_tables();
	desc_table("test");

	struct LIST *data = l_create();
	int id = 1036101;
	char str[MAX_STR_LENGTH] = "Yankai";
	char ss[MAX_STR_LENGTH] = "Male";
	
	l_append(data, INT_TYPE, (VARHANDLE)&id);
	l_append(data, STR_TYPE, (VARHANDLE)str);
	l_append(data, STR_TYPE, (VARHANDLE)ss);
	insert_record("test", data);

	l_delete(data, STR_TYPE, (VARHANDLE)str);
	l_delete(data, STR_TYPE, (VARHANDLE)ss);
	strcpy(str, "Who");
	l_append(data ,STR_TYPE, (VARHANDLE)str);
	l_append(data, STR_TYPE, (VARHANDLE)ss);
	insert_record("test", data);

	struct OPNODE tree, lch, rch;
	tree.lchild = &lch, tree.rchild = &rch;
	tree.op = eql;
	lch.lchild = lch.rchild = NULL;
	rch.lchild = rch.rchild = NULL;
	lch.op = ident, rch.op = conststr;
	lch.iType = 2, rch.iType = STR_TYPE;
	char *ccol = "name", *vval = "Who";
	lch.pValue = (void*)ccol, rch.pValue = (void*)vval;

	delete_record("test", &tree);
	
	l_remove(data, data->pTail);
	l_append(data, STR_TYPE, (VARHANDLE)ss);
	insert_record("test", data);

	l_release(data);
	data = l_create();
	id = 123;
	l_append(data, INT_TYPE, (VARHANDLE)&id);
	l_append(data, STR_TYPE, (VARHANDLE)str);
	l_append(data, STR_TYPE, (VARHANDLE)ss);
	insert_record("test", data);

	return 0;
}
